package cn.edu.nuc.film.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.FilmCity;
import cn.edu.nuc.film.entity.FilmType;
import cn.edu.nuc.film.entity.Remain;
import cn.edu.nuc.film.entity.Screens;
import cn.edu.nuc.film.entity.User;
import cn.edu.nuc.film.service.impl.ImgServiceImpl;
import cn.edu.nuc.film.service.interfaces.FilmService;
import cn.edu.nuc.film.service.interfaces.FilmTypeService;
import cn.edu.nuc.film.service.interfaces.RemainService;
import cn.edu.nuc.film.service.interfaces.ScreensService;

@Controller
@RequestMapping(value="/film/film")
public class FilmController {
	
	@Autowired
	private FilmService filmService;
	
	@Autowired
	private FilmTypeService filmTypeService;
	
	@Autowired
	private ScreensService screensService;
	
	@Autowired
	private RemainService remainService;
	
	/*购买操作*/
	/**
	 * 进入film的购买界面
	 * @param filmId
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/filmstore",method=RequestMethod.GET)
	public String filmstore( int filmId,Model model,HttpServletRequest request){
			
			//获取影厅信息
			List<Screens> screensList = screensService.onlyList(2);
			
			//获取指定影院、影厅、影片的remain表的信息
			//remainList   ---  传递获取remain表里的关于指定影院与影片的影厅的<余票>信息( 主要是获取--->余票)
			List<Remain> remainList = remainService.list(2,filmId);
			
			//如果不为控 , 则执行添加操作
			if( remainList.size() == 0){
				
				for( int i = 0;i<screensList.size();i++){
					//将影厅的信息写进remain的数据表里
					remainService.addRemain( screensList.get(i),filmId);
				}
				
			}else{
				
				for( int i = 0;i<screensList.size();i++){
					//将获取的影厅的余票 set 到 screens 对象里
					screensList.get(i).setRemain(remainList.get(i).getFilmRemain());
					screensList.get(i).setGid(remainList.get(i).getGid());
				}
				
			}
			
			//screensList  ---  传递获取screens表里的关于影院的影厅的信息( 主要是获取---><影厅名>)
			model.addAttribute("screensList", screensList);
			
			//获取film信息
			Film film = filmService.pageFilmDetail(filmId);
			//film  ---   传递获取的film的信息( 主要是获取film的票价、语言)
			model.addAttribute("film", film);
			
			return "/page/film_store";
		
	}
	
	//准备购买操作
	@RequestMapping(value="/shoppage",method=RequestMethod.GET)
	public String shoppage( int filmId,int gid,int screensid,Model model,HttpServletRequest request){
			
			//获取user信息
			User user = (User) request.getAttribute("user");
			System.out.println("user" + user);
			//  需  加判断  用户是否登录  ？？？？
			
			
			Film film = filmService.pageFilmDetail(filmId);
			
			//int gid = Integer.parseInt(request.getParameter("gid"));
			System.out.println("fdf" +gid);
			Remain remain = remainService.remain(gid);
			System.out.println("fdf" + remain);
			//传值
			//request.setAttribute("user", user);
			model.addAttribute("user", user);
			model.addAttribute("film", film);
			model.addAttribute("fcId", 2);
			model.addAttribute("screensid", screensid);
			//request.setAttribute("remain", remain);
			model.addAttribute("remain", remain);
			return "/page/store_shop";
	
			
			
			//接受fcId 的参数  (????? 和 screensid) 先不传fcid
			/*int fcId = Integer.parseInt(request.getParameter("fcId"));*/
			//int screensid = Integer.parseInt(request.getParameter("screensid"));
			
			/*
				 //获取对应的对象信息     可以过来
				 Screens screens = screensService.detail(screensid);
			 */
			//request.setAttribute("screens", screens);
		
	}
	
	/**
	 * 保存影片所属影城
	 * @param roleid
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/saveFilmCity",method=RequestMethod.POST)
	public String saveright( int filmId, HttpServletRequest request){
		
		String[] fcids = request.getParameterValues("ckrr");
		System.out.println(fcids);  
		//删除角色权限
		filmService.deleteCityFilm(filmId);
		//执行
		int count1 = filmService.saveFilmCity(filmId,fcids);
		if( count1 > 0){
			return "redirect:filmlist";
		}else{
			request.setAttribute("msg", "保存影片-影城失败");
			return "sys/film/right";
		}
		
	}
	
	/**
	 * 获取影片影城权限
	 * @param filmId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/initFilmCity",method=RequestMethod.GET)
	public String initFilmCity( int filmId,Model model){
		
		//调用获取角色详细信息的方法（主要用来显示filmCityname以及通过filmId来进行编辑film的功能）
		Film film = filmService.detail(filmId);
		
		//获取角色的权限
		List<FilmCity> list = filmService.initFilmCity(film);
	    //将获取的film功能存放到list集合里进行提交到jsp界面
		model.addAttribute("list", list);
		//返回film的信息
		model.addAttribute("film", film);
		return "sys/film/right";
			

	}
	
	
	/**
	 * 获取filmlist的分页信息(即影片列表)
	 * 并通过影片名进行查询
	 * 还可进行模糊查询(通过影片)
	 * @return
	 */
	@RequestMapping(value="/filmlist",method=RequestMethod.GET)
	public String filmList( String sname,HttpServletRequest request){
		
		//获取要查询的影片名字
		//String sname = request.getParameter("sname");
		if( sname == null || sname.equals("")){
			sname = null;
		}
		/*System.out.println("sname--" + sname);*/
		//设置分页信息
		PageControl pc = new PageControl();
		
		//设置初始当前页currindex = 1
		Integer currindex = 1;
		
		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if(request.getParameter("index")!=null){
			currindex = Integer.parseInt(request.getParameter("index"));
		}
		
		//将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);
		
		//调用service中的分页方法，获取分页信息
		Pager<Film> pager = filmService.filmList(pc, sname);
		
		//将分页信息传送到前台
		request.setAttribute("pager", pager);
		
		//返回filmlist页面
		return "sys/film/filmlist";
	}
	

	/**
	 * 进入添加影片的界面
	 * @return
	 */
	@RequestMapping(value="/toInsertFilm")
	public String toAddFilm(Model model){
		//获取filmType
		List<FilmType> typelist =  filmTypeService.typelist();
		model.addAttribute("typelist", typelist);
		return "sys/film/add";
	}
	/**
	 * 添加成功后，进入影片列表界面
	 * @return
	 */
	@RequestMapping(value="/insertFilm",method=RequestMethod.POST)
	public String insertFilm(Film film,
			@RequestParam("picture") MultipartFile picture,
			@RequestParam("cover") MultipartFile cover,
			HttpServletRequest request,Model model){
		
		//添加图片
		String path = request.getServletContext().getRealPath("/assets/img");
		String filename=ImgServiceImpl.imgpath(path, picture);
		String filmcover=ImgServiceImpl.imgpath(path, cover);
		if(filename!=null){
		  film.setFilmPicture(filename);
		}
		if(filename!=null){
	      film.setFilmCover(filmcover);
		}
		int count = filmService.insert(film);
		if( count > 0){
			
			return "redirect:filmlist";
		}else{
			model.addAttribute("msg", "添加失败！");
			return "sys/film/add_film";
		}
		
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="")
	public String detail(){
		return "";
	}
	
	
	/**
	 * 进入修改界面
	 * @return
	 */
	@RequestMapping(value="/toEditFilm",method=RequestMethod.GET)
	public String toEditFilm(int filmId,Model model){//HttpSession session
		
		//获取filmType
		List<FilmType> typelist =  filmTypeService.typelist();
		model.addAttribute("typelist", typelist);
		//获取修改影片的信息
		Film film = filmService.detail(filmId);
		System.out.println(film);
		//session.setAttribute("film", film);
		model.addAttribute("film", film);
		return "sys/film/edit";
	}
	@RequestMapping(value="/editFilmSuc")
	public String editFilm(Film film,
			@RequestParam("picture") MultipartFile picture,
			@RequestParam("cover") MultipartFile cover,
			HttpServletRequest request,Model model){
		
		//添加图片
		String path = request.getServletContext().getRealPath("/assets/img");
		String filename=ImgServiceImpl.imgpath(path, picture);
		String filmcover=ImgServiceImpl.imgpath(path, cover);
		if(filename!=null){
			 film.setFilmPicture(filename);
		}
		if(filename!=null){
			 film.setFilmCover(filmcover);
		}
		
		
		int count = filmService.updateByFilmId(film);
		if( count > 0 ){
			return "redirect:filmlist";
		}else{
			return "sys/film/edit";
		}
		
	}
	
	/**
	 * 删除影片，通过影片id
	 * @param filmId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deleteFilm",method=RequestMethod.GET)
	public String deleteFilm(int filmId,Model model){
		int count = filmService.deleteFilm(filmId);
		if( count > 0 ){
			return "redirect:filmlist";
		}else{
			model.addAttribute("deletemsg", "删除失败");
			return "redirect:filmlist";
		}
		
	}
	
	
	/**
	 * 进入初始化界面,进行？？？的初始化
	 * @return
	 */
	@RequestMapping(value="/toAuditFilm")
	public String toAuditFilm(){
		return "sys/film/audit";
	}
	/**
	 * 成功后进入filmlist的界面
	 * @return
	 */
	@RequestMapping(value="/auditFilmSuc")
	public String auditFilm(){
		return "redirect:filmlist";
	}
}
